/*
 * Copyright (C) 2022 xuexiangjys(xuexiangjys@163.com)
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *       http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 *
 */

package com.arthas.psimobile.fragment.home;

import android.graphics.Color;
import android.text.SpannableString;
import android.text.style.RelativeSizeSpan;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import androidx.annotation.NonNull;

import com.arthas.psimobile.core.BaseFragment;
import com.arthas.psimobile.fragment.home.postmail.MailPostFragment;
import com.arthas.psimobile.fragment.home.rentcar.CarRentFragment;
import com.arthas.psimobile.fragment.report.ReportViewEnterpriseFragment;
import com.github.mikephil.charting.animation.Easing;
import com.github.mikephil.charting.charts.PieChart;
import com.github.mikephil.charting.data.PieData;
import com.github.mikephil.charting.data.PieDataSet;
import com.github.mikephil.charting.data.PieEntry;
import com.github.mikephil.charting.formatter.PercentFormatter;
import com.github.mikephil.charting.utils.ColorTemplate;
import com.github.mikephil.charting.utils.MPPointF;
import com.xuexiang.psimobile.R;
import com.xuexiang.psimobile.databinding.FragmentHomePageBinding;
import com.xuexiang.xpage.annotation.Page;
import com.xuexiang.xpage.enums.CoreAnim;
import com.xuexiang.xui.widget.actionbar.TitleBar;

import java.util.ArrayList;
import java.util.List;

@Page(anim = CoreAnim.none)
public class HomePageFragment extends BaseFragment<FragmentHomePageBinding> implements View.OnClickListener {

    PieChart chart;

    @Override
    protected TitleBar initTitle() {
        return null;
    }

    @Override
    protected void initListeners() {
        binding.enterpriseSearch.setOnClickListener(this);
        binding.pageRentCar.setOnClickListener(this);
        binding.pagePostMail.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        int id = v.getId();

        if (id == R.id.enterprise_search) {
            openNewPage(ReportViewEnterpriseFragment.class);
        } else if (id == R.id.page_rent_car) {
            openNewPage(CarRentFragment.class);
        } else if (id == R.id.page_post_mail) {
            openNewPage(MailPostFragment.class);
        }
    }

    @NonNull
    @Override
    protected FragmentHomePageBinding viewBindingInflate(LayoutInflater inflater, ViewGroup container) {
        return FragmentHomePageBinding.inflate(inflater, container, false);
    }

    @Override
    protected void initViews() {
        chart = findViewById(R.id.my_credit);
        initChartStyle();
        initChartLabel();

        setChartData(88);

        chart.animateY(1400, Easing.EaseInOutQuad);
    }

    /**
     * 初始化图表样式
     */
    protected void initChartStyle() {
        chart.setUsePercentValues(true);
        chart.getDescription().setEnabled(false);
        chart.setExtraOffsets(6, 10, 5, 5);

        chart.setDragDecelerationFrictionCoef(0f);

        chart.setDrawCenterText(true);
        chart.setDrawHoleEnabled(true);

        chart.setHoleRadius(55f);
        chart.setHoleColor(Color.WHITE);

        chart.setTransparentCircleColor(Color.WHITE);
        chart.setTransparentCircleAlpha(110);
        chart.setTransparentCircleRadius(55f);

        chart.setRotationAngle(180);
        chart.setRotationEnabled(false);
    }

    /**
     * 设置图表标题
     */
    protected void initChartLabel() {
        chart.getLegend().setEnabled(false);
    }


    /**
     * 设置图表数据
     * @param num
     */
    protected void setChartData(int num) {
        List<PieEntry> entries = new ArrayList<>();
        entries.add(new PieEntry(100 - num));
        entries.add(new PieEntry(num));

        PieDataSet dataSet = new PieDataSet(entries, "Election Results");
        dataSet.setDrawIcons(false);
        dataSet.setSliceSpace(3f);
        dataSet.setIconsOffset(new MPPointF(0, 40));
        dataSet.setSelectionShift(5f);

        List<Integer> colors = new ArrayList<>();
        colors.add(Color.GRAY);
        colors.add(Color.rgb(41, 158, 227));
        dataSet.setColors(colors);

        PieData data = new PieData(dataSet);
//        data.setValueFormatter(new PercentFormatter(chart));
        data.setDrawValues(false);
//        data.setValueTextSize(0f);
//        data.setValueTextColor(Color.WHITE);
        chart.setData(data);

        // undo all highlights
        chart.highlightValues(null);
        chart.invalidate();

        chart.setCenterText(generateCenterSpannableText(num));
        chart.setDrawCenterText(true);
    }

    /**
     * 生成饼图中间文字
     */
    private SpannableString generateCenterSpannableText(int num) {
        SpannableString s = new SpannableString("" + num);
        s.setSpan(new RelativeSizeSpan(1.7f), 0, s.length(), 0);
        return s;
    }

}
